package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithThree() {
        //先查询出所有分类数据
        List<CategoryEntity> entitieList = baseMapper.selectList(null);
        //1.组装数据
        //获取父节点
        List<CategoryEntity> leaveFmenue = entitieList.stream().filter(
                categoryEntity -> categoryEntity.getParentCid() == 0
        ).map(categoryEntity -> {
            categoryEntity.setChildren(getChildrens(categoryEntity,entitieList));
            return categoryEntity;
        }).sorted((n1,n2)->{
            return (n1.getSort()==null?0:n1.getSort()) - (n2.getSort()==null?0:n2.getSort());
        }).collect(Collectors.toList());
        return leaveFmenue;
    }

    @Override
    public void deleteByIds(List<Long> asList) {
        //TODO 删除之前需要判断是否有子节点
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCategoryPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);
        Collections.reverse(parentPath);

        return parentPath.toArray(new Long[parentPath.size()]);
    }

    public List<Long> findParentPath(Long catelogId,List<Long> paths){
        paths.add(catelogId);
        CategoryEntity categoryEntity = this.getById(catelogId);
        if (categoryEntity.getParentCid()!=0){
            findParentPath(categoryEntity.getParentCid(),paths);
        }
        return paths;
    }

    public List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> allList){
        List<CategoryEntity> categoryList = allList.stream().filter(
                categoryEntity -> categoryEntity.getParentCid().equals(root.getCatId())
        ).map(categoryEntity -> {
            categoryEntity.setChildren(getChildrens(categoryEntity,allList));
            return categoryEntity;
        }).sorted((n1,n2)->{
            return (n1.getSort()==null?0:n1.getSort()) - (n2.getSort()==null?0:n2.getSort());
        }).collect(Collectors.toList());
        return categoryList;
    }

    @Transactional
    @Override
    public void updateDetail(CategoryEntity category) {
        this.updateById(category);
        if (StringUtils.isNotEmpty(category.getName())) {
            //更新其它关联品牌的名称
            categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
            //TODO  后续需要完善字段保持一致的
        }

    }

}